# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import EMA
from ..base import OpsFeature

from .close import Close

class DIFF(OpsFeature):
    """Technical Indicator - DIFF

    - DIFF = EMA(CLOSE, 12) - EMA(CLOSE, 26)
    """
    def __init__(self, feature=None, short=12, long=26):
        self.feature = feature or Close()
        self.short = short
        self.long = long

    def __str__(self):
        name = type(self).__name__.lower()
        if not isinstance(self.feature, Close):
            name += '_'+str(self.feature)
        if self.short != 12 or self.long != 26:
            name += '_{}_{}'.format(self.short, self.long)
        return name
                                           
    def _load_internal(self, instrument, start_time, end_time, freq):
        diff = EMA(self.feature, self.short) - EMA(self.feature, self.long)
        return diff.load(instrument, start_time, end_time, freq)

    
class MACD(OpsFeature):
    """Technical Indicator - MACD

    - DEA  = EMA(DIFF, 9)
    - MACD = DIFF - DEA
    """
    def __init__(self, feature=None, short=12, long=26, N=9):
        self.feature = feature or Close()
        self.short = short
        self.long = long
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if not isinstance(self.feature, Close):
            name += '_'+str(self.feature)
        if self.short != 12 or self.long != 26 or self.N != 9:
            name += '_{}_{}_{}'.format(self.short, self.long, self.N)
        return name
                                           
    def _load_internal(self, instrument, start_time, end_time, freq):
        diff = DIFF(self.feature, self.short, self.long)
        dea = EMA(diff, self.N)
        macd = (diff - dea) * 2
        return macd.load(instrument, start_time, end_time, freq)
